package com.yuesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.CustomException;
import com.yuesystem.entity.ChangeItem1;
import com.yuesystem.entity.ChangeItem2;
import com.yuesystem.entity.OfficeItem;
import com.yuesystem.mapper.ChangeItem2Mapper;
import com.yuesystem.service.ChangeItem1Service;
import com.yuesystem.service.ChangeItem2Service;
import com.yuesystem.service.OfficeItemService;
import com.yuesystem.service.OfficeService;
import com.yuesystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 改变书院条目接口实现类.
 */
@Service
@Slf4j
public class ChangeItem2ServiceImpl extends ServiceImpl<ChangeItem2Mapper, ChangeItem2>
    implements ChangeItem2Service {
  @Autowired
  private OfficeItemService officeItemService;
  
  @Autowired
  private OfficeService officeService;
  
  @Autowired
  private UserService userService;
  
  @Autowired
  private ChangeItem1Service changeItem1Service;
  
  /**
   * 增加条目信息，保存在office_item表和change_item2表中.
   */
  @Override
  @Transactional
  public void addItem(OfficeItem officeItem) {
    if (officeItem.getObject().isEmpty() || officeItem.getSubject().isEmpty()
        || officeItem.getRelation().isEmpty()) {
      throw new CustomException("新增条目不能为空！");
    }
    LambdaQueryWrapper<OfficeItem> query = new LambdaQueryWrapper<>();
    query.eq(OfficeItem::getObject, officeItem.getObject())
        .eq(OfficeItem::getSubject, officeItem.getSubject())
        .eq(OfficeItem::getRelation, officeItem.getRelation())
        .eq(OfficeItem::getOfficeId, officeItem.getOfficeId())
        .eq(OfficeItem::getIsDeleted, 0);
    OfficeItem one = officeItemService.getOne(query);
    if (one != null) {
      throw new CustomException("该条目已存在，请重新输入！");
    } else {
      officeItemService.save(officeItem);
    }
    LambdaQueryWrapper<OfficeItem> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(OfficeItem::getSubject, officeItem.getSubject())
        .eq(OfficeItem::getObject, officeItem.getObject())
        .eq(OfficeItem::getRelation, officeItem.getRelation())
        .eq(OfficeItem::getOfficeId, officeItem.getOfficeId())
        .eq(OfficeItem::getIsDeleted, 0);
    OfficeItem item = officeItemService.getOne(queryWrapper);
    log.info("增加条目信息:{}", item);
    // 获得当前登录用户id
    Long currentId = BaseContext.getCurrentId();
    ChangeItem1 changeItem1 = new ChangeItem1();
    changeItem1.setSubjectNew(officeItem.getSubject());
    changeItem1.setObjectNew(officeItem.getObject());
    changeItem1.setRelationNew(officeItem.getRelation());
    changeItem1.setUserId(currentId);
    changeItem1.setOperate("增加");
    log.info("增加操作信息 :{}", changeItem1);
    changeItem1Service.save(changeItem1);
  }
  
  /**
   * 删除academy_item表中该条目的信息，保存用户删除信息在change_item表中.
   *
   */
  @Override
  @Transactional
  public void deleteItem(OfficeItem officeItem) {
    // 从office_item表中删除该条目的信息
    officeItem.setIsDeleted(1);
    // 保存修改后的officeItem信息
    officeItemService.updateById(officeItem);
    ChangeItem1 changeItem1 = new ChangeItem1();
    changeItem1.setSubjectNew(officeItem.getSubject());
    changeItem1.setObjectNew(officeItem.getObject());
    changeItem1.setRelationNew(officeItem.getRelation());
    changeItem1.setUserId(BaseContext.getCurrentId());
    changeItem1.setOperate("删除");
    log.info("新增删除信息： {}", changeItem1);
    changeItem1Service.save(changeItem1);
  }
  
  /**
   * 修改官职条目信息，对change_item2和office_item表同时进行操作.
   *
   */
  @Override
  @Transactional
  public void updateItem(OfficeItem officeItem) {
    if (officeItem.getObject().isEmpty() || officeItem.getRelation().isEmpty()) {
      throw new CustomException("修改条目不能为空！");
    }
    Long itemId = officeItem.getId();
    OfficeItem item = officeItemService.getItemById(itemId);
    ChangeItem1 changeItem1 = new ChangeItem1();
    changeItem1.setSubjectOld(item.getSubject());
    changeItem1.setObjectOld(item.getObject());
    changeItem1.setRelationOld(item.getRelation());
    changeItem1.setSubjectNew(officeItem.getSubject());
    changeItem1.setObjectNew(officeItem.getObject());
    changeItem1.setRelationNew(officeItem.getRelation());
    changeItem1.setUserId(BaseContext.getCurrentId());
    changeItem1.setOperate("修改");
    changeItem1Service.save(changeItem1);
    officeItemService.updateById(officeItem);
    
  }
  
}
